var Tarjetas = Class.create();

Tarjetas.prototype = {
	initialize : function(url) {
		this.porTipos = new Hash();
		this.fichas = [];
		this.idx = -1;

		new Ajax.Request(url, {
			asynchronous :false,
			onSuccess : function(transport) {
				$A(transport.responseXML.getElementsByTagName("grupo")).each(function(g){
					var fichas = new Array();
					this.porTipos.set(g.getAttribute("nombre"), fichas);
					
					$A(g.getElementsByTagName("tarjeta")).each(function(f){
						fichas.push({
							palabra: f.getAttribute("palabra"), 
							traduccion: f.getAttribute("traduccion"),
							ejemplo: f.getAttribute("ejemplo"),
						});
					}.bind(this));

				}.bind(this));

			}.bind(this)
		});

		this.porTipos.values().each(function(f){
			f.each(function(i){
				this.fichas.push(i);
			}.bind(this));
		}.bind(this));

	},
	
	current : function () {
		return this.idx >= 0 && this.idx < this.fichas.length ? this.fichas[this.idx] : {};
	},
	
	next : function() {
		return this.hasNext() ? this.fichas[++this.idx] : {};
	},
	
	hasNext : function() {
		return this.idx < this.fichas.length-1;
	},
	
	previous : function() {
		return this.hasPrevious() ? this.fichas[--this.idx] : {};
	},
	
	hasPrevious : function() {
		return this.idx > 0;
	},
	
	tipos : function() {
		return this.porTipos.keys().sort();
	}
	
}
